<?php
require_once("../../../config.inc.php");
$isReloadable = true;
require_once("../../../app/_inc/include.php");
require_once("../_inc/procFunction.php");

$errMsg = "";
$module = sxREQ('mod');
$name = sxREQ('name');

if($module=='' || $name=='') {
	$url = $_SESSION[USEROBJ]->getHistory(0, true).
		"&msg=".urlencode(_t("missing name '$module/$name'"));
	serverRedir($url);
}

if(sxREQ("reqtype") == "delete"){
	$errMsg .= crulDel();
}
$sSQL = "SELECT * FROM com_rule "
	." WHERE name='".sxRAS("name")."'" 
	." AND module='".sxRAS("mod")."'";
$objSDAO->query($sSQL,"test");
if(!$row=$objSDAO->fetch('test')) {
	$url = $_SESSION[USEROBJ]->getHistory(-1, true).
		"&msg=".urlencode(_t("missing name '$module/$name'"));
	serverRedir($url);
}

$sSQL = "SELECT cra.*, e.description AS e_des FROM com_rule_alias cra " 
	." LEFT JOIN entity e ON e.entity = cra.entity "
	." WHERE cra.name='".sxRAS("name")."'" 
	." AND cra.module='".sxRAS("mod")."'";
$objSDAO->query($sSQL, "selAl");

sxRDef('description', $row['description']);
sxRDef('note', $row['note']);
sxRDef('admitted_sub', $row['admitted_sub']);
sxRDef('upgrade_status', $row['upgrade_status']);
sxRDef('script', $row['script']);

$aOpz = array("kMenu"=>'pro.cru', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
function conDel(){
	if(confirm("Confirm?"=)) {
		document.location.href = "<?="crulMod.php?mod=$module&name=".sxREQ('name')."&xsn=".XSN?>";
	}
}
</script>
<?php
echo(bodyHTML($aOpz));

echo getModHeader($module);

$aCmd = array("del"=>array("pag"=>"javascript:conDel()",
										"title"=>_t("delete"),
										"text"=>_t("delete")
										),
					"chg"=>array("pag"=>"crulMod.php?mod=$module&name=".sxREQ('name'),
										"title"=>_t("change"),
										"text"=>_t("change")
										),
					"back"=>array()
					);
$sCmd = commandsHTML($aCmd);	
echo $sCmd;

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='4'>Common Rule</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width=\"20%\">Name</td>" .
			"<td class=\"text01\" width=\"10%\">".getHtmlVisText('name')."</td>" .
			"<td class=\"labl01\" width=\"15%\">Description</td>" .
			"<td class=\"text01\" width=\"55%\">".getHtmlVisText('description')."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Subroutine allowed</td>" .
			"<td class=\"text01\">".getHtmlVisDecode('admitted_sub', array('N'=>'no','Y'=>'yes'))."</td>" .
			"<td class=\"labl01\">Upgrade status</td>" .
			"<td class=\"text01\">".getHtmlVisDecode('upgrade_status', getUpgradeStatusVal())."</td>" .
		"</tr>\n");
if (sxRTrim('note')!='') {
	echo("<tr>" .
				"<td class=\"labl01\">Note</td>" .
				"<td class=\"text01\" colspan='2'>".getHtmlPreText('note')."</td>" .
			"</tr>\n");
}
echo("</table>");
echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
//aliases
$aCmd = array("pag"=>"crulAliasIns.php?mod=$module&name=$name",
						"title"=>_t("add new alias"),
						"text"=>_t("new alias"),
						"perm"=>"viewmod",
						"align"=>"R");
echo getTitleLink('List of Aliases', $aCmd);
echo("<tr>" .
		"<td class=\"tabh01\" width=\"1%\">"._t('Alias')."</td>" .
		"<td class=\"tabh01\" width=\"1%\">"._t('R/O')."</td>" .
		"<td class=\"tabh01\" width=\"40%\">"._t('Entity')."</td>" .
		"<td class=\"tabh01\" width=\"58%\">"._t('Description')."</td>" .
	"</tr>\n");	
$numElem=0;
$aAliasXYW['W'] = array('W - simple memory variable','not-exists');
$aAliasXYW['X'] = array('X - 1 dimension array memory variables','not-exists');
$aAliasXYW['Y'] = array('Y - 2 dimensions array memory variables','not-exists');
while($row = $objSDAO->fetch("selAl")){
	$numElem ++;
	if (isset($aAliasXYW[$row["com_alias"]])) {
		$aAliasXYW[$row["com_alias"]][1] = 'exists';
		continue;
	}
	echo("<tr>" .
			"<td class=\"text01\" valign='top' align='center'><a class=\"text01\" ".
				goToPag("crulAliasView.php?mod=$module&name=$name&alias=".$row["com_alias"]."&xsn=".XSN).
				" title=\""._t("Visualizza alias")."'".sxHE($row["description"])."'\">&nbsp;".
				$row["com_alias"]."&nbsp;</a></td>\n" .
			"<td class=\"text01\" valign='top'>".(($row["read_only"]=='Y')?'R/O':'&nbsp;')."</td>" .
			"<td class=\"text01\" valign='top'><a class=\"text01\" ".
				goToPag("../../data/enty/entityView.php?da=".substr($row["entity"],0,4)."&ent=".$row["entity"]."&xsn=".XSN).
				" title=\""._t("View entity")."'\">".
				sxHE($row["entity"])."</a></td>" .
			"<td class=\"text01\" valign='top'>".sxHE(ucfirst($row['e_des']).
				((empty($row['description']))?'':" - ".$row['description']))."</td>" .
		"</tr>\n");
}
echo("<tr>" .
			"<td class=\"text01\" colspan=\"20\">");
//dbg_($aAliasXYW);
foreach($aAliasXYW as $kX => $aX) {
	echo "<a class=\"text01\" ".
				goToPag("crulAliasView.php?mod=$module&name=$name&alias=$kX&xsn=".XSN).
				" title=\"Visualizza alias $kX\">".$aX[0]."</a>" .
				"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	if ($aX[1]=='not-exists') { //alias not defined
		$sSql = "INSERT INTO com_rule_alias (module,name,com_alias,entity,description,note,read_only)"
			." values('$module','$name','$kX','','".addslashes(SUBSTR($aX[0],4))."','','N')";
		//dbg_($sSql);
		$objSDAO->exec($sSql);
	}
}
echo 		"</td>".
		"</tr>\n";
echo("</table>\n");
//script
echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
$aCmd = array("pag"=>"crulScript.php?mod=$module&name=$name",
						"title"=>_t("edit script"),
						"text"=>_t("edit script"),
						"perm"=>"viewmod",
						"align"=>"R");
echo getTitleLink('Script', $aCmd);
echo("<tr>" .
			"<td class=\"text01\">".getHtmlPreScript('script')."&nbsp;</td>" .
		"</tr>\n");
echo("</table>");
echo $sCmd;
echo(footerHTML());
exit();

function crulDel(){
	global $objSDAO;
	$errMsg = "";

	if($errMsg != ""){return $errMsg;}		
	
	$sSQL = "DELETE FROM com_rule " .
				"WHERE name = '".sxRAS('name')."' AND module='".sxRAS('mod')."'";
	$objSDAO->exec($sSQL);
	$url = $_SESSION[USEROBJ]->getHistory(-1, true);
	serverRedir($url);
	return 'redir err';
}
